/**
 * 手写一个Redux，先看使用，再进行判断怎么写
 */


const initialState = {
    name: 'wnagbo',
    age: 10
}

const todoRedux = (state = initialState, data) => {
    switch (data.type) {
        case 'xxx':
            return {...state, xxx: data.data}
        default:
            return state
    }
}

function createStore(reducer) {
    let state, handlers = []
    return {
        getState() {
            return state
        },
        dispatch(action) {
            state = reducer(state, action)
            handlers.forEach(handler => handler())
        },
        subscribe(handler) {
            handlers.push(handler)
//            handler.apply(this)
        }
    }
}


const store = createStore(todoRedux)
store.subscribe(() => console.log(store.getState()))
store.dispatch({type: 'xxx', data: 12345})
